/**
 * 
 */
package com.surelution.lansao.wxmp

import com.surelution.lansao.Member
import com.surelution.lansao.MemberAddress;
import com.surelution.lansao.WaitingFor
import com.surelution.whistle.core.Attribute;

/**
 * @author <a href="mailto:guangzong.syu@gmail.com">Guangzong</a>
 *
 */
class WaitingForAction extends BaseLansaoAction {
	
	private WaitingFor waitingFor

	/* (non-Javadoc)
	 * @see com.surelution.whistle.core.BaseAction#accept()
	 */
	@Override
	public boolean accept() {
		waitingFor = null
		if(getParam(Attribute.KEY_MsgType) == "text" && getParam(Attribute.KEY_Content)) {
			def cri = WaitingFor.createCriteria() 
			def l = cri.list() {
				eq('member', member)
				eq('finished', false)
				ge('expiredAt', new Date())
			}
			if(l && l.size() > 0) {
				waitingFor = l[0]
				return true
			}
		}
		false
	}

	/* (non-Javadoc)
	 * @see com.surelution.whistle.core.BaseAction#execute()
	 */
	@Override
	public void execute() {
//		waitingFor.finished = true
		def m = Member.get(member.id)
		def address = getParam(Attribute.KEY_Content)
		MemberAddress ma = new MemberAddress(member:m, address:address, verified:false, lastDelivery:false)
		ma.save(flush:true)
//		waitingFor.save(flush:true)
//		m.save(flush:true)
		put(new Attribute(Attribute.KEY_Content, "我们收到您发送来的地址:${address}，我们的工作人员会立即验证您的地址，并随后发送确认消息给您。谢谢您的支持！"))
	}

}
